package com.teaut.dota.hider.boot;

import com.teaut.dota.hider.entity.MenuEntity;
import com.teaut.dota.hider.entity.TenantEntity;
import com.teaut.dota.hider.entity.UserEntity;
import com.teaut.dota.hider.entity.common.MenuType;
import com.teaut.dota.hider.entity.common.Status;
import com.teaut.dota.hider.repository.MenuEntityRepository;
import com.teaut.dota.hider.repository.TenantInfoRepository;
import com.teaut.dota.hider.repository.UserInfoRepository;
import java.util.Date;

import com.teaut.dota.utils.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;




@Component
public class BootApplicationRunner implements ApplicationRunner{
    public static final String HR = "=====================================================";
    @Autowired
    MenuEntityRepository menuEntityRepository;
    @Autowired
    TenantInfoRepository tenantInfoRepository;

    @Autowired
    UserInfoRepository userInfoRepository;
    @Override
    public void run(ApplicationArguments args)  {


        TenantEntity tenant = new TenantEntity();
        tenant.setId("0");
        tenant.setName("总部");
        tenant.setPhone("13888888888");
        tenant.setAddress("中国");
        tenant.setId("0");

        UserEntity user = initUser(tenant);
        tenant.setCreator(user);
        tenant.setTenant(tenant);
        tenantInfoRepository.save(tenant);

        initMenu(user,tenant);
        System.out.print(HR);
        System.out.print("系统启动完成");
        System.out.println(HR);
    }

    private UserEntity initUser(TenantEntity tenant) {
        System.out.println("初始化超管用户");
        UserEntity user = new UserEntity();
        user.setId("0");
        user.setUsername("admin");
        user.setPassword("admin");
        user.setCreator(user);
        user.setEmail("root@root.com");
        user.setPhone("13912345678");
        user.setAddress("三环中心古工国际5号院");
        user.setCity("北京");
        user.setState("北京");
        user.setZip("100000");
        user.setCountry("中国");
        user.setLanguage("ZH_CN");
        user.setTimezone("+8");
        user.setTemplate("Dark");
        user.setTenant(tenant);
        userInfoRepository.save(user);
        return user;
    }

    private void initMenu(UserEntity user, TenantEntity tenant) {
        System.out.println("初始化菜单");
        MenuEntity menu = new MenuEntity();
        menu.setId("0");
        menu.setName("根节点");
        menu.setDescr("根节点");
        menu.setUrl("/");
        menu.setIcon("start");
        menu.setType(MenuType.DIRECTORY);
        menu.setState(Status.ENABLE);
        menu.setParent(menu);
        menu.setCreator(user);
        menu.setTenant(tenant);
        menuEntityRepository.save(menu);
    }
}
